package com.example.mountainwalker.controller;

import com.example.mountainwalker.entity.UserFollowInfo;
import com.example.mountainwalker.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mw")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
public class UserFollowController {
    @Autowired
    private UserFollowService userFollowService;

    //http://localhost:8080/mw/userFollowList
    //http://localhost:8080/mw/userFollowList?username=xxx&followedUsername=xxx&startTime=xxx&endTime=xxx
    @GetMapping("/userFollowList")
    public List<UserFollowInfo> getUserFollowList(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String followedUsername,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {

        if (username != null || followedUsername != null || startTime != null || endTime != null) {
            return userFollowService.getUserFollowListByCondition(username, followedUsername, startTime, endTime);
        }
        return userFollowService.getUserFollowList();
    }

    //http://localhost:8080/mw/userFollowById?id=1
    @GetMapping("/userFollowById")
    public UserFollowInfo getUserFollowById(@RequestParam Integer id) {
        return userFollowService.getUserFollowById(id);
    }

    //http://localhost:8080/mw/deleteUserFollowById?id=1
    @DeleteMapping("/deleteUserFollowById")
    public int deleteUserFollowById(@RequestParam Integer id) {
        return userFollowService.deleteUserFollowById(id);
    }

    //http://localhost:8080/mw/batchDeleteUserFollow
    @DeleteMapping("/batchDeleteUserFollow")
    public int batchDeleteUserFollow(@RequestBody List<Integer> ids) {
        return userFollowService.batchDeleteUserFollow(ids);
    }

    //http://localhost:8080/mw/addUserFollow
    @PostMapping("/addUserFollow")
    public int addUserFollow(@RequestBody UserFollowInfo userFollowInfo) {
        userFollowInfo.setFollowedTime(new Date());
        return userFollowService.addUserFollow(userFollowInfo);
    }

    //http://localhost:8080/mw/updateUserFollow
    @PutMapping("/updateUserFollow")
    public int updateUserFollow(@RequestBody UserFollowInfo userFollowInfo) {
        return userFollowService.updateUserFollow(userFollowInfo);
    }


}